package com.base;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.base.repository.BasicRepository;
import com.common.AccessableOperationObject;
import com.common.enumeration.OperationObjectType;
import com.iptv.invoker.InvokerClient;


/**
 * 权限接口定义
 * @author lyw
 * @date 2014-05-08 10:09:33
 * @version 1.0
 *
 */
public abstract class Operation<T> {
	private String id;
	private String code;
	private String name;
	private String description;
	private BasicRepository basicRepository;
	private List<Right> rights;

	// getters and setters start
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}
	// getters and setters start end
	
	public List<Right> getRights() {
		return rights;
	}

	public void setRights(List<Right> rights) {
		this.rights = rights;
	}

	public abstract Object doOperation(OperationObject operationObject);
	
	// base business operation, commonly it contact with database.
	public boolean add(T t) {
//		basicRepository.save(t);
		return true;
	}
	public boolean delete(T t) {
//		basicRepository.delete(t);
		return true;
	}
	public boolean modify(T t) {
//		basicRepository.merge(t);
		return true;
	}
	public List<OperationObject> retrieve(T t) {
		System.out.println("exec retrieve ...");
		return null;
	}
	public List<OperationObject> find(T t) {
		return null;
	}
	public boolean batchModify(T t) {
		return true;
	}
	public boolean batchDelete(T t) {
		return true;
	}
	public boolean batchAdd(T t) {
		return true;
	}
	public boolean access(T t) {
		AccessableOperationObject object = (AccessableOperationObject) t;
		String sourceUrl = object.getSourceURL();
		String publishUrl = object.getPublishURL();
		
		// post request to media server via http interface.
		boolean result = InvokerClient.request();
		
		System.out.println("exec access ...");
		return true;
	}
	public boolean expand(T t, Map parameters) {
		
//		basicRepository.select(hql, paramters);
		
		return true;
	}
	
	protected Map expandOperationObject(OperationObject operationObject) {
		// get child nodes from database.
		OperationObjectType expandChildOperationObjectType = operationObject.getExpandChildOperationObjectType();
		Iterator<Right> it = this.rights.iterator();
		while (it.hasNext()) {
			Right right = it.next();
			String rCode = right.getOperationObject().getCode();
		}

		System.out.println("exec access ...");
		
		return new HashMap<String, String>();
	}
	
}
